package br.edu.unifei.cct720.alieninvasion.utils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Class to manipulate highest scores file
 * 
 * @author Felipe Knappe
 *
 */
public class HighestScoreFile extends File {

	/** The stream buffer to read data from file */
	private BufferedReader streamReader = null;

	/** The stream buffer to write data on file */
	private BufferedWriter streamWriter = null;

	/**
	 * Class Constructor
	 * Creates a new stream to read / write data from
	 * scores file.
	 * 
	 * @throws IllegalArgumentException
	 */
	public HighestScoreFile(String a) throws IllegalArgumentException {
		super(a);
	}

	/**
	 * Method responsible for initialize read stream buffer.
	 * 
	 * @return BufferedReader
	 * @throws IOException
	 */
	public BufferedReader openStreamReader() throws IOException {
		this.streamReader = new BufferedReader(new FileReader(this.getAbsolutePath()));
		return this.streamReader;
	}

	/**
	 * Method responsible for initialize write stream buffer.
	 * 
	 * @return BufferedReader
	 * @throws IOException
	 */
	public BufferedReader openStreamWriter() throws IOException {
		this.streamReader = new BufferedReader(new FileReader(this.getAbsolutePath()));
		return this.streamReader;
	}

	/**
	 * Method responsible for close read stream buffer.
	 * 
	 * @throws IOException, NullPointerException
	 */
	public void closeStreamReader() throws IOException, NullPointerException {
		this.streamReader.close();
		this.streamReader = null;
	}

	/**
	 * Method responsible for close write stream buffer.
	 * 
	 * @throws IOException, NullPointerException
	 */
	public void closeStreamWriter() throws IOException, NullPointerException {
		this.streamWriter.close();
		this.streamWriter = null;
	}

	/**
	 * Method responsible for retrieve highest scores from 
	 * file.
	 * 
	 * @return Integer[]
	 * @throws IOException
	 */
	public List<Integer> getHighestScores() throws IOException {
		List<Integer> highestScores = new ArrayList<Integer>();
		String line = "";

		if(this.isReadyToRead()) {
			for (int i = 0; ((line = this.streamReader.readLine()) != null) && (i < 5); i++) {
				highestScores.add(Integer.parseInt(line));
			}
		}
		return highestScores;
	}

	/**
	 * Method responsible for update and save highest scores 
	 * the game on file.
	 * 
	 * @param value
	 * @throws IOException
	 */
	public boolean setHighestScores(List<Integer> highestScores) throws IOException {
		if(this.isReadyToWrite()) {
			for(int i = 0; i < highestScores.size(); i++) {
				streamWriter.write(highestScores.get(i).toString());
				streamWriter.newLine();
			}
			return true;
		}
		return false;
	}

	/**
	 * Method responsible for check when stream buffer
	 * are ready to read file data.
	 * 
	 * @return boolean
	 * @throws IOException
	 */
	private boolean isReadyToRead() throws IOException {
		if(this.streamReader == null || !this.streamReader.ready())
			this.openStreamReader();
		return true;	
	}

	/**
	 * Method responsible for check when stream writer
	 * are ready to write file data.
	 * 
	 * @return boolean
	 * @throws IOException
	 */
	private boolean isReadyToWrite() throws IOException {
		if(this.streamWriter == null)
			this.openStreamWriter();
		return true;
	}
}
